package com.unknown.wificontrol;

import android.view.KeyEvent;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.aspsine.fragmentnavigator.FragmentNavigator;
import com.unknown.wificontrol.adapter.MainTabAdapter;
import com.unknown.wificontrol.ui.BaseActivity;
import com.unknown.wificontrol.ui.home.HomeFragment;
import com.unknown.wificontrol.ui.images.DashboardFragment;
import com.unknown.wificontrol.widget.TabTitleItem;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import me.majiajie.pagerbottomtabstrip.NavigationController;
import me.majiajie.pagerbottomtabstrip.PageNavigationView;
import me.majiajie.pagerbottomtabstrip.listener.OnTabItemSelectedListener;

public class MainActivity extends BaseActivity implements OnTabItemSelectedListener {

    @BindView(R.id.tab_view)
    PageNavigationView nav_tabbar;

    @Override
    public int getLayout() {
        return R.layout.activity_main;
    }

    private FragmentNavigator mNavigator;

    @Override
    public void initViews() {
        NavigationController navigationController = nav_tabbar.custom()
                .addItem(new TabTitleItem(this, R.drawable.ic_home_normal, R.drawable.ic_home_high, "参数设置"))
                .addItem(new TabTitleItem(this, R.drawable.ic_images_normal, R.drawable.ic_images_high, "成像结果"))
                .build();
        List<Fragment> list = new ArrayList<>();
        list.add(new HomeFragment());
        list.add(new DashboardFragment());
        navigationController.addTabItemSelectedListener(this);
        mNavigator = new FragmentNavigator(getSupportFragmentManager(), new MainTabAdapter(list), R.id.realcontent);
        mNavigator.setDefaultPosition(0);
        mNavigator.showFragment(0);
    }

    @Override
    public void onSelected(int index, int old) {
        mNavigator.showFragment(index);
    }

    @Override
    public void onRepeat(int index) {

    }

    private long mExitTime;

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - mExitTime) > 2000) {
                showToast("再按一次返回退出!");
                mExitTime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
            }
            return false;
        }
        return super.dispatchKeyEvent(event);
    }
}